<?php

namespace Biz\service\settlement;

use Biz\module\Classes\SettlementTransaction as clsSettlementTransaction;
use Doraemon\tools\Log as toolsLog;
use Biz\pockets\serviceTrait\_Async;
use Doraemon\amqp\Queue;
use League\Flysystem\Exception;

/**
 * Class Settlement
 * 订单结算
 * @package Biz\service\settlement
 * @deprecated
 */
class SettlementTransaction
{
    use _Async;

    /**
     * 成功提交订单流程
     * @param $shopId 店铺id
     * @param $orderNo 订单no
     * @param $integralAmount 积分抵扣金额
     * @param $couponAmount 平台优惠券金额
     * @param $activityAllowance 平台活动补贴
     * @param $packetAmount 红包抵扣金额
     * @return bool
     * @throws \Exception
     */
    public static function afterCreateOrder($shopId, $orderNo, $integralAmount, $couponAmount, $activityAllowance, $packetAmount)
    {
        return self::_async($shopId, $orderNo, $integralAmount, $couponAmount, $activityAllowance, $packetAmount);
    }

    public static function _async_afterCreateOrder($shopId, $orderNo, $integralAmount, $couponAmount, $activityAllowance, $packetAmount)
    {
        $operateTime = date('Y-m-d H:i:s');
        try {
            if (!floatval($integralAmount) && !floatval($couponAmount) && !floatval($activityAllowance) && !floatval($packetAmount)) {
                return false;
            }
            return (new clsSettlementTransaction())->afterCreateOrder($shopId, $orderNo, $integralAmount, $couponAmount, $activityAllowance, $packetAmount, $operateTime);
        } catch (\Exception $e) {
            toolsLog::settlement_afterCreateOrder_warning('提交订单后结算流程出错', [
                'function' => 'afterCreateOrder',
                'params' => [$shopId, $orderNo, $integralAmount, $couponAmount, $activityAllowance, $packetAmount, $operateTime],
                'e_msg' => $e->getMessage(),
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine()
            ]);
            Queue::insertQueue("queue/Wechatnotice/toTag", [
                "totag" => \Biz\pockets\constInterface\BizWechatTagConfig::TAG_BIZ_RD_SYSTEM,
                "content" => "提交订单后结算流程出错:#msg:" . $e->getMessage() . "#file:" . $e->getFile() . "#line:" . $e->getLine() . "#params:" . json_encode(func_get_args(), JSON_UNESCAPED_UNICODE)
            ], Queue::QUEUE_BIZ);
            throw $e;
        }
    }

    /**
     * 取消订单成功
     * @param $shopId 店铺id
     * @param $orderNo 订单no
     * @param $integralAmount 积分抵扣金额或则
     * @param $couponAmount 平台优惠券金额
     * @param $activityAllowance 平台活动补贴
     * @param $packetAmount 红包抵扣金额
     * @return bool
     * @throws \Exception
     */
    public static function afterCancelOrder($shopId, $orderNo, $integralAmount, $couponAmount, $activityAllowance, $packetAmount)
    {
        return self::_async($shopId, $orderNo, $integralAmount, $couponAmount, $activityAllowance, $packetAmount);
    }

    public static function _async_afterCancelOrder($shopId, $orderNo, $integralAmount, $couponAmount, $activityAllowance, $packetAmount)
    {
        $operateTime = date('Y-m-d H:i:s');
        try {
            if (!floatval($integralAmount) && !floatval($couponAmount) && !floatval($activityAllowance) && !floatval($packetAmount)) {
                return false;
            }
            return (new clsSettlementTransaction())->afterCancelOrder($shopId, $orderNo, $integralAmount, $couponAmount, $activityAllowance, $packetAmount, $operateTime);
        } catch (\Exception $e) {
            toolsLog::settlement_afterCancelOrder_warning('取消订单后结算流程出错', [
                'function' => 'afterCancelOrder',
                'params' => [$shopId, $orderNo, $integralAmount, $couponAmount, $activityAllowance, $packetAmount, $operateTime],
                'e_msg' => $e->getMessage(),
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine()
            ]);
            Queue::insertQueue("queue/Wechatnotice/toTag", [
                "totag" => \Biz\pockets\constInterface\BizWechatTagConfig::TAG_BIZ_RD_SYSTEM,
                "content" => "取消订单后结算流程出错:#msg:" . $e->getMessage() . "#file:" . $e->getFile() . "#line:" . $e->getLine() . "#params:" . json_encode(func_get_args(), JSON_UNESCAPED_UNICODE)
            ], Queue::QUEUE_BIZ);
            throw $e;
        }
    }

    /**
     *  订单支付成功
     * @param $shopId 店铺id
     * @param $orderNo 订单no
     * @param $transactionAmount 支付金额payamount
     * @param string $payWay 支付方式
     * @param float $shippingAmount 运费
     * @return bool
     * @throws \Exception
     */
    public static function afterPaySuccess($shopId, $orderNo, $transactionAmount, $payWay = '', $shippingAmount = 0.00)
    {
        return self::_async($shopId, $orderNo, $transactionAmount, $payWay, $shippingAmount);
    }

    public static function _async_afterPaySuccess($shopId, $orderNo, $transactionAmount, $payWay, $shippingAmount)
    {
        $operateTime = date('Y-m-d H:i:s');
        try {
            if (floatval($transactionAmount) < 0 || floatval($shippingAmount) < 0) {
                throw new Exception('订单支付成功后结算流程出错，支付金额或则运费小于0');
            }
            return (new clsSettlementTransaction())->afterPaySuccess($shopId, $orderNo, $transactionAmount, $operateTime, $payWay, $shippingAmount);
        } catch (\Exception $e) {
            toolsLog::settlement_afterPaySuccess_warning('订单支付成功后结算流程出错', [
                'function' => 'afterPaySuccess',
                'params' => [$shopId, $orderNo, $transactionAmount, $operateTime, $payWay, $shippingAmount],
                'e_msg' => $e->getMessage(),
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine()
            ]);
            Queue::insertQueue("queue/Wechatnotice/toTag", [
                "totag" => \Biz\pockets\constInterface\BizWechatTagConfig::TAG_BIZ_RD_SYSTEM,
                "content" => "订单支付成功后结算流程出错:#msg:" . $e->getMessage() . "#file:" . $e->getFile() . "#line:" . $e->getLine() . "#params:" . json_encode(func_get_args(), JSON_UNESCAPED_UNICODE)
            ], Queue::QUEUE_BIZ);
            throw $e;
        }
    }


    /**
     * 确认收货
     * @param $shopId 店铺id
     * @param $orderNo 订单no
     * @param $transactionAmount 订单交易实付金额payamount
     * @param $integralAmount 订单交易积分支付金额
     * @param $couponAmount 订单交易平台优惠券支付金额
     * @param $activityAllowance 平台活动补贴
     * @param $packetAmount 红包抵扣金额
     * @param $commissions 订单佣金
     * @param $payWay 支付方式
     * @param $shippingAmount 运费
     * @param date $confirmTime 确认收货时间
     * @param bool $isSys 是否系统操作
     * @return bool
     * @throws \Exception
     */
    public static function afterConfirmOrder($shopId, $orderNo, $transactionAmount, $integralAmount, $couponAmount, $activityAllowance, $packetAmount, $commissions, $payWay, $shippingAmount, $confirmTime, $isSys = false)
    {
        return self::_async($shopId, $orderNo, $transactionAmount, $integralAmount, $couponAmount, $activityAllowance, $packetAmount, $commissions, $payWay, $shippingAmount, $confirmTime, $isSys);
    }

    public static function _async_afterConfirmOrder($shopId, $orderNo, $transactionAmount, $integralAmount, $couponAmount, $activityAllowance, $packetAmount, $commissions, $payWay, $shippingAmount, $confirmTime, $isSys)
    {
        try {
            return (new clsSettlementTransaction())->afterConfirmOrder($shopId, $orderNo, $transactionAmount, $integralAmount, $couponAmount, $activityAllowance, $packetAmount, $commissions, $payWay, $shippingAmount, $confirmTime, $isSys);
        } catch (\Exception $e) {
            toolsLog::settlement_afterConfirmOrder_warning('订单确认收货后结算流程出错', [
                'function' => 'afterConfirmOrder',
                'params' => [$shopId, $orderNo, $transactionAmount, $integralAmount, $couponAmount, $activityAllowance, $packetAmount, $commissions, $payWay, $shippingAmount, $confirmTime, $isSys],
                'e_msg' => $e->getMessage(),
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine()
            ]);
            Queue::insertQueue("queue/Wechatnotice/toTag", [
                "totag" => \Biz\pockets\constInterface\BizWechatTagConfig::TAG_BIZ_RD_SYSTEM,
                "content" => "订单确认收货后结算流程出错:#msg:" . $e->getMessage() . "#file:" . $e->getFile() . "#line:" . $e->getLine() . "#params:" . json_encode(func_get_args(), JSON_UNESCAPED_UNICODE)
            ], Queue::QUEUE_BIZ);
            throw $e;
        }
    }

    /**
     * 退款成功交易流程
     * @param $shopId
     * @param $orderNo
     * @param $refundOrderNo
     * @param int $type (1:确认收货前退款2:确认收货后退款)
     * @param int $goodsId
     * @param int $skuId
     * @param $goodsName 商品名称-sku名称
     * @param $transactionAmount 退款金额 支付金额payamount
     * @param $shippingAmount 退运费金额
     * @param $integralAmount 返积分金额
     * @param $couponAmount 返平台优惠券金额
     * @param $activityAllowance 返平台活动补贴
     * @param $packetAmount 返红包抵扣金额
     * @param date $confirmTime 确认退款成功时间
     * @param $payWay 支付方式
     * @param bool $isSys 是否系统操作
     * @param float | int $commissions 返佣金金额
     * @throws \Exception
     * @return bool
     */
    public static function afterRefundSuccess($shopId, $orderNo, $refundOrderNo, $type = 1, $goodsId, $skuId, $goodsName, $transactionAmount, $shippingAmount, $integralAmount, $couponAmount, $activityAllowance, $packetAmount, $confirmTime, $payWay, $isSys = false, $commissions = 0)
    {
        return self::_async($shopId, $orderNo, $refundOrderNo, $type, $goodsId, $skuId, $goodsName, $transactionAmount, $shippingAmount, $integralAmount, $couponAmount, $activityAllowance, $packetAmount, $confirmTime, $payWay, $isSys, $commissions);
    }

    public static function _async_afterRefundSuccess($shopId, $orderNo, $refundOrderNo, $type, $goodsId, $skuId, $goodsName, $transactionAmount, $shippingAmount, $integralAmount, $couponAmount, $activityAllowance, $packetAmount, $confirmTime, $payWay, $isSys, $commissions)
    {
        try {
            if (!floatval($transactionAmount) && !$type) {
                return false;
            }

            return (new clsSettlementTransaction())->afterRefundSuccess($shopId, $orderNo, $refundOrderNo, $type, $goodsId, $skuId, $goodsName, $transactionAmount, $shippingAmount, $integralAmount, $couponAmount, $activityAllowance, $packetAmount, $confirmTime, $payWay, $isSys, $commissions);
        } catch (\Exception $e) {
            toolsLog::settlement_afterRefundSuccess_warning('订单退款成功后结算流程出错', [
                'function' => 'afterRefundSuccess',
                'params' => [$shopId, $orderNo, $refundOrderNo, $type, $goodsId, $skuId, $goodsName, $transactionAmount, $shippingAmount, $integralAmount, $couponAmount, $activityAllowance, $packetAmount, $confirmTime, $payWay, $isSys, $commissions],
                'e_msg' => $e->getMessage(),
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine()
            ]);
            Queue::insertQueue("queue/Wechatnotice/toTag", [
                "totag" => \Biz\pockets\constInterface\BizWechatTagConfig::TAG_BIZ_RD_SYSTEM,
                "content" => "订单退款成功后结算流程出错:#msg:" . $e->getMessage() . "#file:" . $e->getFile() . "#line:" . $e->getLine() . "#params:" . json_encode(func_get_args(), JSON_UNESCAPED_UNICODE)
            ], Queue::QUEUE_BIZ);
            throw $e;
        }
    }

    /**
     *
     * 提现成功后结算流程
     * @param $shopId
     * @param $transactionAmount 订单提现的金额
     * @param $transNumber 银行交易流水号
     * @param $operateTime 操作时间
     * @param $operateAdminId 操作人id
     * @param $remark 备注
     * @return bool
     * @throws \Exception
     */
    public static function afterWithdrawal($shopId, $transactionAmount, $transNumber, $operateTime, $operateAdminId, $remark)
    {
        return self::_async($shopId, $transactionAmount, $transNumber, $operateTime, $operateAdminId, $remark);
    }

    public static function _async_afterWithdrawal($shopId, $transactionAmount, $transNumber, $operateTime, $operateAdminId, $remark)
    {
        try {
            if (floatval($transactionAmount) < 0) {
                throw new Exception('店铺提现成功后结算流程出错，提现金额小于0');
            }
            return (new clsSettlementTransaction())->afterWithdrawal($shopId, $transactionAmount, $transNumber, $operateTime, $operateAdminId, $remark);
        } catch (\Exception $e) {
            toolsLog::settlement_afterWithdrawal_warning('店铺提现成功后结算流程出错', [
                'function' => 'afterWithdrawal',
                'params' => [$shopId, $transactionAmount, $transNumber, $operateTime, $operateAdminId, $remark],
                'e_msg' => $e->getMessage(),
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine()
            ]);
            Queue::insertQueue("queue/Wechatnotice/toTag", [
                "totag" => \Biz\pockets\constInterface\BizWechatTagConfig::TAG_BIZ_RD_SYSTEM,
                "content" => "店铺提现成功后结算流程出错:#msg:" . $e->getMessage() . "#file:" . $e->getFile() . "#line:" . $e->getLine() . "#params:" . json_encode(func_get_args(), JSON_UNESCAPED_UNICODE)
            ], Queue::QUEUE_BIZ);
            throw $e;
        }
    }

    /**
     * 运营调整-调整佣金
     * @param int $shopId 店铺id
     * @param float $transactionAmount 调整金额
     * @param int $type （1 :补款 2：扣款）
     * @param $transNumber 银行交易流水号
     * @param $operateTime
     * @param $operateAdminId
     * @param $remark
     * @return bool
     * @throws \Exception
     */
    public static function businessAdjustment($shopId, $transactionAmount, $type = 1, $transNumber, $operateTime, $operateAdminId, $remark)
    {
        return self::_async($shopId, $transactionAmount, $type, $transNumber, $operateTime, $operateAdminId, $remark);
    }

    public static function _async_businessAdjustment($shopId, $transactionAmount, $type, $transNumber, $operateTime, $operateAdminId, $remark)
    {
        try {
            if (!floatval($transactionAmount) && !$type) {
                return false;
            }

            return (new clsSettlementTransaction())->businessAdjustment($shopId, $transactionAmount, $type, $transNumber, $operateTime, $operateAdminId, $remark);
        } catch (\Exception $e) {
            toolsLog::settlement_businessAdjustment_warning('运营调整-调整佣金结算流程出错', [
                'function' => 'businessAdjustment',
                'params' => [$shopId, $transactionAmount, $type, $transNumber, $operateTime, $operateAdminId, $remark],
                'e_msg' => $e->getMessage(),
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine()
            ]);
            Queue::insertQueue("queue/Wechatnotice/toTag", [
                "totag" => \Biz\pockets\constInterface\BizWechatTagConfig::TAG_BIZ_RD_SYSTEM,
                "content" => "运营调整-调整佣金结算流程出错:#msg:" . $e->getMessage() . "#file:" . $e->getFile() . "#line:" . $e->getLine() . "#params:" . json_encode(func_get_args(), JSON_UNESCAPED_UNICODE)
            ], Queue::QUEUE_BIZ);
            throw $e;
        }
    }

    /**
     * 运营调整-调整平台补贴
     * @param int $shopId 店铺id
     * @param float $transactionAmount 调整金额
     * @param int $type （1 :补款 2：扣款）
     * @param $transNumber 银行交易流水号
     * @param $operateTime
     * @param $operateAdminId
     * @param $remark
     * @return bool
     * @throws \Exception
     */
    public static function hhzAllowanceAdjustment($shopId, $transactionAmount, $type = 1, $transNumber, $operateTime, $operateAdminId, $remark)
    {
        return self::_async($shopId, $transactionAmount, $type, $transNumber, $operateTime, $operateAdminId, $remark);
    }

    public static function _async_hhzAllowanceAdjustment($shopId, $transactionAmount, $type, $transNumber, $operateTime, $operateAdminId, $remark)
    {
        try {
            if (!floatval($transactionAmount) && !$type) {
                return false;
            }

            return (new clsSettlementTransaction())->hhzAllowanceAdjustment($shopId, $transactionAmount, $type, $transNumber, $operateTime, $operateAdminId, $remark);
        } catch (\Exception $e) {
            toolsLog::settlement_hhzAllowanceAdjustment_warning('运营调整-调整平台补贴结算流程出错', [
                'function' => 'hhzAllowanceAdjustment',
                'params' => [$shopId, $transactionAmount, $type, $transNumber, $operateTime, $operateAdminId, $remark],
                'e_msg' => $e->getMessage(),
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine()
            ]);
            Queue::insertQueue("queue/Wechatnotice/toTag", [
                "totag" => \Biz\pockets\constInterface\BizWechatTagConfig::TAG_BIZ_RD_SYSTEM,
                "content" => "运营调整-调整平台补贴结算流程出错:#msg:" . $e->getMessage() . "#file:" . $e->getFile() . "#line:" . $e->getLine() . "#params:" . json_encode(func_get_args(), JSON_UNESCAPED_UNICODE)
            ], Queue::QUEUE_BIZ);
            throw $e;
        }
    }

    /**
     * 保证金补货款（扣完保证金调用）
     * @param $shopId 店铺id
     * @param $transactionAmount 保证金补货款金额
     * @param $transNumber 银行交易流水号
     * @param $operateTime
     * @param $operateAdminId
     * @param $remark
     * @throws
     * @return bool
     */
    public static function depositAddGoodsAmount($shopId, $transactionAmount, $transNumber, $operateTime, $operateAdminId, $remark)
    {
        return self::_async($shopId, $transactionAmount, $transNumber, $operateTime, $operateAdminId, $remark);
    }

    public static function _async_depositAddGoodsAmount($shopId, $transactionAmount, $transNumber, $operateTime, $operateAdminId, $remark)
    {
        try {
            if (!floatval($transactionAmount)) {
                return false;
            }

            return (new clsSettlementTransaction())->depositAddGoodsAmount($shopId, $transactionAmount, $transNumber, $operateTime, $operateAdminId, $remark);
        } catch (\Exception $e) {
            toolsLog::settlement_depositAddGoodsAmount_warning('保证金补货款结算流程出错', [
                'function' => 'depositAddGoodsAmount',
                'params' => [$shopId, $transactionAmount, $transNumber, $operateTime, $operateAdminId, $remark],
                'e_msg' => $e->getMessage(),
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine()
            ]);
            Queue::insertQueue("queue/Wechatnotice/toTag", [
                "totag" => \Biz\pockets\constInterface\BizWechatTagConfig::TAG_BIZ_RD_SYSTEM,
                "content" => "保证金补货款结算流程出错:#msg:" . $e->getMessage() . "#file:" . $e->getFile() . "#line:" . $e->getLine() . "#params:" . json_encode(func_get_args(), JSON_UNESCAPED_UNICODE)
            ], Queue::QUEUE_BIZ);
            throw $e;
        }
    }

    /**
     * 商家交保证金后结算流程 todo unit test 添加引用(是否需要记录到结算log)
     * @param $shopId
     * @param $transactionAmount
     * @param $transNumber 银行交易流水号
     * @param $operateTime
     */
    public static function addDeposit($shopId, $transactionAmount, $transNumber, $operateTime)
    {
        return self::_async($shopId, $transactionAmount, $transNumber, $operateTime);
    }

    public static function _async_addDeposit($shopId, $transactionAmount, $transNumber, $operateTime)
    {
        try {
            if (!floatval($transactionAmount)) {
                return false;
            }

            return (new clsSettlementTransaction())->addDeposit($shopId, $transactionAmount, $transNumber, $operateTime);
        } catch (\Exception $e) {
            toolsLog::settlement_addDeposit_warning('商家交保证金结算流程出错', [
                'function' => 'addDeposit',
                'params' => [$shopId, $transactionAmount, $transNumber, $operateTime],
                'e_msg' => $e->getMessage(),
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine()
            ]);
            throw $e;
        }
    }

    /**
     * 关店退保成功后结算流程操作 todo unit test 添加引用(是否需要记录到结算log)
     * @param $shopId
     * @param $transactionAmount
     * @param $transNumber 银行交易流水号
     * @param $operateTime
     * @return bool
     * @throws \Exception
     */
    public static function closeAndBackDeposit($shopId, $transactionAmount, $transNumber, $operateTime)
    {
        return self::_async($shopId, $transactionAmount, $transNumber, $operateTime);
    }

    public static function _async_closeAndBackDeposit($shopId, $transactionAmount, $transNumber, $operateTime)
    {
        try {
            if (!floatval($transactionAmount)) {
                return false;
            }

            return (new clsSettlementTransaction())->closeAndBackDeposit($shopId, $transactionAmount, $transNumber, $operateTime);
        } catch (\Exception $e) {
            toolsLog::settlement_closeAndBackDeposit_warning('关店退保结算流程出错', [
                'function' => 'closeAndBackDeposit',
                'params' => [$shopId, $transactionAmount, $transNumber, $operateTime],
                'e_msg' => $e->getMessage(),
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine()
            ]);
            throw $e;
        }
    }

}